package com.woniuxy.woniuhis.config;

import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.cache.MemoryConstrainedCacheManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.woniuxy.woniuhis.realm.UserRealm;



@Configuration
public class ShiroConfig {
	@Bean
	public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager) {
		ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
		shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);

		return shiroFilterFactoryBean;
	}

	@Bean
	public DefaultWebSecurityManager defaultWebSecurityManager(UserRealm userRealm) {
		DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
		defaultWebSecurityManager.setRealm(userRealm);
		return defaultWebSecurityManager;
	}

	// 注入Bean的属性值，直接将返回其他方法的方法名注入，
	// 由于方法名就是bean的名字，Bean需要的属性值，容器就直接根据属性名直接注入，
	// 代码组成方法签名中就是其他Bean的方法名
	@Bean
	public UserRealm userRealm(/* CredentialsMatcher credentialsMatcher, */CacheManager cacheManager) {
		UserRealm userRealm = new UserRealm();
		//userRealm.setCredentialsMatcher(credentialsMatcher);
		userRealm.setCacheManager(cacheManager);
		return userRealm;
	}

	// 凭证匹配器
//	@Bean
//	public HashedCredentialsMatcher credentialsMatcher() {
//		HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
//		credentialsMatcher.setHashAlgorithmName("MD5");
//		credentialsMatcher.setHashIterations(2);
//		return credentialsMatcher;
//	}

	// 使用内置缓存,缓存管理器，这样，再验证是否有某个权限时，只需要查一次，然后缓存起来
	@Bean
	public MemoryConstrainedCacheManager cacheManager() {
		return new MemoryConstrainedCacheManager();
	}
}
